#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QString url = QString("https://d1.weather.com.cn/wap40/101220801.html?_=%1").arg(QDateTime::currentDateTime().toTime_t());
    QNetworkRequest* request = new QNetworkRequest(url);
    request->setRawHeader(QByteArray("referer"),  QByteArray("http://m.weather.com.cn"));
    request->setRawHeader(QByteArray("user-agent"), "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69");
    QNetworkAccessManager* accessManager = new QNetworkAccessManager();
    QNetworkReply* reply =accessManager->get(*request);
    connect(reply, SIGNAL(finished()), this, SLOT(finished_slot()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::finished_slot(){
    QNetworkReply* reply = (QNetworkReply*)sender();
    bt = QString(reply->readAll()).toHtmlEscaped().replace("&quot;","\"").replace("var wap40 = ", "").toUtf8();
    reply->deleteLater();
    QJsonDocument doc = QJsonDocument::fromJson(bt);
    QJsonObject ob = doc.object();


    QString city = ob["city"].toString();
    list = ob["datas"].toVariant().toList();


    ui->city_label->setText(city);
    QDateTime time = QDateTime::currentDateTime();
    ui->year->display(time.date().year());
    ui->month->display(time.date().month());
    ui->day->display(time.date().day());

    QMap<QString, QVariant> li_map =  list[0].toMap();
    QString maxTemp = li_map["max"].toString();
    QString minTemp =  li_map["min"].toString();
    QString weather = li_map["weather"].toString();
    QString pixMap = QString(":/weather/D:/image/%1.png").arg(weather,1);

    ui->max_temp->display(maxTemp);
    ui->min_temp->display(minTemp);
    ui->weather_pix->setPixmap(QPixmap(pixMap));
    ui->weather_label->setText(weather);


}

